﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;
using System.Threading.Tasks;

namespace STF.OrangeDog.Service
{
    public class CommentBll:ICommentBll
    {
        private ICommentDal _dal;

        public CommentBll(ICommentDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddCommentsAsync(int Bid, string Content, int GoodPraise, int Uid)
        {
            return await _dal.AddModelAsync(new Comment
            {
                bid = Bid,
                content = Content,
                GoodPraise = GoodPraise,
                uid = Uid
            });
        }

        public async Task<int> DeleteCommentsAsync(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;
            return await _dal.RemoveModelAsync(data);
        }

        public async Task<int> EditCommentsAsync(int Id, int Bid, string Content, int GoodPraise, int Uid)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.bid = Bid;
            data.content = Content;
            data.GoodPraise = GoodPraise;
            data.uid = Uid;
            
            return await _dal.EditModelAsync(data);
        }

        public async Task<IEnumerable<CommentDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(r => new CommentDtos
            {
                id = r.Id,
                bid = r.bid,
                content = r.content,
                GoodPraise = r.GoodPraise,
                uid = r.uid,
               
            }) : new List<CommentDtos>();
        }

        public async Task<CommentDtos> GetDataById(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            return data == null ? new CommentDtos() : new CommentDtos
            {
                id = data.Id,
                bid = data.bid,
                content = data.content,
                GoodPraise = data.GoodPraise,
                uid = data.uid,
                
            };
        }
    }
}
